using System;
using System.Drawing;

using SdlDotNet.Graphics;

using Game.Output;
using Game.Utils;

namespace Game.Data
{
	public class CTileset
	{
		private const int MAX_SPRITES_PER_TILESET = 1000;
		
		public struct structSpriteInfo 
		{
			public int xPos;
			public int yPos;
			public int width;
			public int height;
			public int xRel;
			public int yRel;
		}
		
		private structSpriteInfo[] m_spriteInfos;
		
		private string	m_name;
		private Surface	m_surface;
		
		private static int m_instanceCounter;
		
		public CTileset(string p_name)
		{
			CConsole.ReportClassInit(System.Reflection.MethodInfo.GetCurrentMethod().DeclaringType.Name);
			CConsole.ReportParam("p_name",  p_name);
			
			m_name = p_name;			
			
			m_instanceCounter++;
			
			LoadInfoFile();
			
			LoadSurface();
		}
		
		public void LoadInfoFile()
		{
			String[]	words;
			String		lineTrimmed;
			int			lineNr = 0;
			int			spriteNr = 0;
			CTextFile	textFile;
			
			m_spriteInfos = new structSpriteInfo[MAX_SPRITES_PER_TILESET];
			
			string path = CProgram.ROOT_DIR + @"data\tilesets\" + m_name + ".txt";
			
			textFile = new CTextFile(path);
			textFile.LoadLines();
			
			foreach (String line in textFile.Lines)
			{		
				if (lineNr == 0)
					continue;
				
				lineTrimmed  = line.Trim();
				words = lineTrimmed.Split('\t');
				
				spriteNr = Convert.ToInt32(words[0]);
				
				if (spriteNr >= MAX_SPRITES_PER_TILESET)
					break;
				
				m_spriteInfos[spriteNr].xPos		= Convert.ToInt32(words[1]);
				m_spriteInfos[spriteNr].yPos		= Convert.ToInt32(words[2]);
				m_spriteInfos[spriteNr].width		= Convert.ToInt32(words[3]);
				m_spriteInfos[spriteNr].height		= Convert.ToInt32(words[4]);
				m_spriteInfos[spriteNr].xRel		= Convert.ToInt32(words[5]);
				m_spriteInfos[spriteNr].yRel		= Convert.ToInt32(words[6]);
				
				lineNr++;
			}	
		}
		
		private void LoadSurface()
		{
			m_surface = new Surface(CProgram.ROOT_DIR + @"data\tilesets\" + m_name + ".bmp");
			
			m_surface.Transparent = true;
			m_surface.TransparentColor = Color.FromArgb(0, 0, 0, 255);
		}
		
		# region properties
		
		public string Name
		{
			get {
				return m_name;
			}
		}
		
		public Surface Surface
		{
			get {
				return m_surface;
			}
		}
		
		public structSpriteInfo[] SpriteInfos
		{
			get {
				return m_spriteInfos;
			}
		}
		
		# endregion
		
	}
}
